package com.starsola.soufang.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@Configuration  // 配置
// 指定仓库类位置，让他可以扫描到
@EnableJpaRepositories(basePackages = "com.starsola.soufang.repository")
// 开启事务
@EnableTransactionManagement
public class JPAConfig {

    @Bean
    // 设置数据库 的属性
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }


    // 实体类的管理工厂
    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() {
        HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
        // 是否生成 SQL，把SQL 控制权掌握在自己手里
        jpaVendorAdapter.setGenerateDdl(false);

    }


}
